import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:wallpaper/components/AlertDialog/my_loading.dart';

class ImageLoad extends StatelessWidget {
  final String imageUrl;
  final BoxFit fit;

  const ImageLoad({super.key, required this.imageUrl, this.fit = BoxFit.cover});

  @override
  Widget build(BuildContext context) {
    return ExtendedImage.network(
      imageUrl,
      width: double.infinity,
      height: double.infinity, // 屏幕高度
      fit: BoxFit.cover,
      cache: true,
      mode: ExtendedImageMode.gesture,
      initGestureConfigHandler: (state) {
        return GestureConfig(
          // 缩放最小值
          minScale: 1,
          // 缩放动画最小值 当缩放结束时回到 minScale 值
          animationMinScale: 1,
          // 缩放最大值
          maxScale: 1,
          // 缩放动画最大值 当缩放结束时回到 maxScale 值
          animationMaxScale: 1,
          // 缩放拖拽速度
          speed: 1.0,
          // 拖拽惯性速度
          inertialSpeed: 100.0,
          initialScale: 1.0,
          // 是否使用 ExtendedImageGesturePageView 展示图片
          inPageView: false,
          // 当图片的初始化缩放大于 1.0 的时候，根据相对位置初始化图片
          initialAlignment: InitialAlignment.center,
        );
      },

      /// 加载状态回调
      loadStateChanged: (ExtendedImageState state) {
        switch (state.extendedImageLoadState) {
          /// 加载中
          case LoadState.loading:
            return MyLoading(
              type: 3,
              size: 30,
            );

          /// 加载成功
          case LoadState.completed:
            return state.completedWidget;

          /// 加载失败
          case LoadState.failed:
            // 自己写的加载失败的组件 并且把重试的回调传递过去
            return Center(
              child: Icon(
                Icons.perm_media,
                color: Theme.of(context).colorScheme.primaryContainer,
                size: 50,
              ),
            );
        }
      },
    );
  }
}
